package promotion

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"bp/internal/util"
	"context"
	"fmt"
)

// GetPromotionPtr 获取促销活动
func (s *PromotionEntityServiceImp) GetPromotionPtr(ctx context.Context, q *sqlc.Queries, promotionID int64) (promotionPtr *sqlc.Promotion, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	promotion, err := q.GetPromotion(ctx, promotionID)
	if err != nil {
		return
	}
	slog.Infof("1 GetPromotion ok")

	promotionPtr = &promotion
	return
}

// ListPromotionPtrsParam 列出促销活动参数
type ListPromotionPtrsParam struct {
	PromotionStatuses []string
	OfferTypes        []string
	Keyword           string
}

// ListCouponPtrs 列出促销活动
func (s *PromotionEntityServiceImp) ListPromotionPtrs(ctx context.Context, q *sqlc.Queries, cPtr *param.ListCommonParam, ptr *ListPromotionPtrsParam) (total int64, promotionPtrs []*sqlc.Promotion, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	total, err = q.CountPromotions(ctx, sqlc.CountPromotionsParams{
		Ts:                       cPtr.PagerPtr.Ts,
		BrandID:                  cPtr.BrandID,
		FilterAllPromotionStatus: len(ptr.PromotionStatuses) == 0,
		PromotionStatuses:        ptr.PromotionStatuses,
		FilterAllOfferType:       len(ptr.OfferTypes) == 0,
		OfferTypes:               ptr.OfferTypes,
		IsKeywordEmpty:           util.IsEmpty(ptr.Keyword),
		Keyword:                  fmt.Sprintf("%%%s%%", ptr.Keyword),
	})
	if err != nil {
		return
	}
	if total == 0 {
		return
	}

	rows, err := q.ListPromotions(ctx, sqlc.ListPromotionsParams{
		Ts:                       cPtr.PagerPtr.Ts,
		BrandID:                  cPtr.BrandID,
		FilterAllPromotionStatus: len(ptr.PromotionStatuses) == 0,
		PromotionStatuses:        ptr.PromotionStatuses,
		FilterAllOfferType:       len(ptr.OfferTypes) == 0,
		OfferTypes:               ptr.OfferTypes,
		IsKeywordEmpty:           util.IsEmpty(ptr.Keyword),
		Keyword:                  fmt.Sprintf("%%%s%%", ptr.Keyword),
		Offset:                   cPtr.PagerPtr.Offset,
		Limit:                    cPtr.PagerPtr.Limit,
	})
	if err != nil {
		return
	}
	slog.Infof("1 ListPromotionPtrs ok")

	promotionPtrs = s.MapPromotionEntityToPtr(rows)
	return
}

// ListPromotionWithStatPtrsParam 列出促销活动及其统计参数
type ListPromotionWithStatPtrsParam struct {
	PromotionStatuses []string
	OfferTypes        []string
	Keyword           string
}

// ListPromotionWithStatPtrs 列出促销活动及其统计
func (s *PromotionEntityServiceImp) ListPromotionWithStatPtrs(ctx context.Context, q *sqlc.Queries, cPtr *param.ListCommonParam, ptr *ListPromotionWithStatPtrsParam) (total int64, promotionWithStatPtrs []*sqlc.ListPromotionWithStatsRow, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	total, err = q.CountPromotions(ctx, sqlc.CountPromotionsParams{
		Ts:                       cPtr.PagerPtr.Ts,
		BrandID:                  cPtr.BrandID,
		FilterAllPromotionStatus: len(ptr.PromotionStatuses) == 0,
		PromotionStatuses:        ptr.PromotionStatuses,
		FilterAllOfferType:       len(ptr.OfferTypes) == 0,
		OfferTypes:               ptr.OfferTypes,
		IsKeywordEmpty:           util.IsEmpty(ptr.Keyword),
		Keyword:                  fmt.Sprintf("%%%s%%", ptr.Keyword),
	})
	if err != nil {
		return
	}
	if total == 0 {
		return
	}

	rows, err := q.ListPromotionWithStats(ctx, sqlc.ListPromotionWithStatsParams{
		Ts:                       cPtr.PagerPtr.Ts,
		BrandID:                  cPtr.BrandID,
		FilterAllPromotionStatus: len(ptr.PromotionStatuses) == 0,
		PromotionStatuses:        ptr.PromotionStatuses,
		FilterAllOfferType:       len(ptr.OfferTypes) == 0,
		OfferTypes:               ptr.OfferTypes,
		IsKeywordEmpty:           util.IsEmpty(ptr.Keyword),
		Keyword:                  fmt.Sprintf("%%%s%%", ptr.Keyword),
		Offset:                   cPtr.PagerPtr.Offset,
		Limit:                    cPtr.PagerPtr.Limit,
	})
	if err != nil {
		return
	}
	slog.Infof("1 ListPromotionWithStatPtrs ok")

	promotionWithStatPtrs = s.MapPromotionWithStatEntityToPtr(rows)
	return
}

// ListAvailablePromotionsOfUseSceneParam 列出场景可用的促销活动参数
type ListAvailablePromotionsOfUseSceneParam struct {
	BrandID  int64
	Issuer   string
	UseScene string
}

// ListAvailablePromotionsOfUseScene 列出场景可用的促销活动
func (s *PromotionEntityServiceImp) ListAvailablePromotionsOfUseScene(ctx context.Context, q *sqlc.Queries, ptr *ListAvailablePromotionsOfUseSceneParam) (promotionPtrs []*sqlc.Promotion, err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	promotions, err := q.ListAvailablePromotionsByBrandIDIssuerAndUseScene(ctx, sqlc.ListAvailablePromotionsByBrandIDIssuerAndUseSceneParams{
		BrandID:  ptr.BrandID,
		Issuer:   ptr.Issuer,
		UseScene: ptr.UseScene,
	})
	if err != nil {
		return
	}
	promotionPtrs = s.MapPromotionEntityToPtr(promotions)
	return
}
